package com.datangnet.parking.basecommon.dao.impl;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.datangnet.parking.basecommon.dao.PublicDao;

@Repository("publicDao")
public class PublicDaoImpl extends AbstractBasicDAO implements PublicDao {
	@SuppressWarnings("unused")
	private static final Logger logger = LoggerFactory.getLogger(PublicDaoImpl.class);
	
	public Object insert(Object entity) {
		return currentSession4Write().save(entity);
	}
	
	public void update(Object entity) {
		currentSession4Write().update(entity);
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getById(Class<T> clazz, Object id) {
		return (T) currentSession4Read().get(clazz, (Serializable) id);
	}

	@Override
	public List<?> queryByHQL(String hql, Map<String, Object> parameters) {
		Query query = currentSession4Read().createQuery(hql);
		query.setProperties(parameters);
		return query.list();
	}
	
	public List<?> queryByHQLWithPaging(String hql, Map<String, Object> parameters, int firstResult, int maxResults) {
		Query query = currentSession4Read().createQuery(hql);
		query.setProperties(parameters);
		query.setFirstResult(firstResult);
		query.setMaxResults(maxResults);
		return query.list();
	}
	
	@Override
	public  List<?> queryBySQLWithPaging(String sql, Map<String, Object> parameters, int firstResult, int maxResults, Class<?> clazz) {
		SQLQuery query = currentSession4Read().createSQLQuery(sql);
		query.setProperties(parameters);
		query.setFirstResult(firstResult);
		query.setMaxResults(maxResults);
		if (clazz!=null) {
			query.setResultTransformer(Transformers.aliasToBean(clazz));
		}
		return query.list();
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public List<?> queryByHQL(String hql, Object[] parameters) {
		Query query = currentSession4Read().createQuery(hql);
		if (parameters!=null && parameters.length>0) {
			for (int i=0; i<parameters.length; i++) {
				query.setParameter(i, parameters[i]);
			}
		}
		List list = query.list();
		return list;
	}

	@Override
	public int executeHQL(String hql, Map<String, Object> parameters) {
		Query query = currentSession4Write().createQuery(hql);
		query.setProperties(parameters);
		return query.executeUpdate();
	}
	
	@Override
	public int executeHQL(String hql, Object[] parameters) {
		Query query = currentSession4Write().createQuery(hql);
		if (parameters!=null && parameters.length>0) {
			for (int i=0; i<parameters.length; i++) {
				query.setParameter(i, parameters[i]);
			}
		}
		return query.executeUpdate();
	}

	@SuppressWarnings("rawtypes")
	@Override
	public List<?> queryBySQL(String sql, Map<String, Object> parameters) {
		Query query = currentSession4Read().createSQLQuery(sql);
		if (parameters!=null && parameters.size()>0) {
			query.setProperties(parameters);
		}
		List list = query.list();
		return list;
	}
	
	@SuppressWarnings("rawtypes")
	@Override
	public List<?> queryBySQL(String sql, Object[] parameters) {
		Query query = currentSession4Read().createSQLQuery(sql);
		if (parameters!=null && parameters.length>0) {
			for (int i=0; i<parameters.length; i++) {
				query.setParameter(i, parameters[i]);
			}
		}
		List list = query.list();
		return list;
	}
	
	@Override
	public Long countByHQL(String hql, Map<String, Object> parameters) {
		Query query = currentSession4Read().createQuery("select count(*) " + hql);
		query.setProperties(parameters);
		Number count = (Number)query.uniqueResult();
		return count.longValue();
	}
	
	@Override
	public Long countByHQL(String hql, Object[] parameters) {
		Query query = currentSession4Read().createQuery("select count(*) " + hql);
		if (parameters!=null && parameters.length>0) {
			for (int i=0; i<parameters.length; i++) {
				query.setParameter(i, parameters[i]);
			}
		}
		Number count = (Number)query.uniqueResult();
		
		return count.longValue();
	}
	
	@Override
	public Long countBySQL(String sql, Map<String, Object> parameters) {
		Query query = currentSession4Read().createSQLQuery("select count(*) " + sql);
		query.setProperties(parameters);
		Number count = (Number)query.uniqueResult();
		return count.longValue();
	}
	
	@Override
	public Long countBySQL(String sql, Object[] parameters) {
		Query query = currentSession4Read().createSQLQuery("select count(*) " + sql);
		if (parameters!=null && parameters.length>0) {
			for (int i=0; i<parameters.length; i++) {
				query.setParameter(i, parameters[i]);
			}
		}
		Number count = (Number)query.uniqueResult();
		
		return count.longValue();
	}

	@Override
	public boolean ifExistedByHQL(String hql, Map<String, Object> parameters) {
		return countByHQL(hql, parameters)>0;
	}

	@Override
	public boolean ifExistedByHQL(String hql, Object[] parameters) {
		return countByHQL(hql, parameters)>0;
	}

}
